/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.model;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import scheduler.databean.User;

import scheduler.model.UserDao;

public class UserDaoBeanFactoryImpl extends GenericDAO<User> implements UserDao {
	public UserDaoBeanFactoryImpl(String jdbcDriverName, String jdbcURL) {
		this(BeanTable.getSQLInstance(User.class,"user",jdbcDriverName,jdbcURL));
	}
	
	private UserDaoBeanFactoryImpl(BeanTable<User> table) {
		super(table,"userName");
        table.setIdleConnectionCleanup(true);
	}
	
	public void setPassword(String userName, String password) throws DAOException {
        try {
        	Transaction.begin();
			User dbUser = getUserByName(userName);
			dbUser.setPassword(password);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public boolean checkPassword(String userName, String password) throws DAOException {
	    User dbUser = getUserByName(userName);
	    return dbUser.checkPassword(password);
	}
	
	public User getUserByName(String userName) throws DAOException {
	   return lookup(userName);
	}
	
}
